package com.gc.service.impl;

import com.gc.entity.*;
import com.gc.pojo.OutExcelBook;
import com.gc.repository.*;
import com.gc.repository.impl.*;
import com.gc.service.DoExcelService;
import org.apache.poi.ss.formula.functions.T;

import java.util.ArrayList;
import java.util.List;


/**
 *    String courseId;
 *     String courseName;
 *     String className;
 *     String studentId;
 *     String studentName;
 *     String ISBN;
 *     String publisher;
 *     String bookName;
 */

/**
 * 拿到excel对象
 */
public class DoExcelServiceImpl implements DoExcelService {


    CourseRepository courseRepository = new CourseRepositoryImpl();
    TeachClassRepository teachClassRepository = new TeachClassRepositoryImpl();
    TextBookRepository textBookRepository = new TextBookRepositoryImpl();
    StudentRepository studentRepository = new StudentRepositoryImpl();
    CheckRepository checkRepository = new CheckRepositoryImpl();
    CcsRepository ccsRepository = new CcsRepositoryImpl();
    StuCheckRepository stuCheckRepository = new StuCheckRepositoryImpl();
    @Override
    public List<OutExcelBook> OutTeachBookByCourseId(String CourseId) {
        Course course = courseRepository.FindByCourseId(CourseId);
        List<TeachClass> teachClasses = teachClassRepository.FindByCourseId(course.getCid());
        List<OutExcelBook> res = new ArrayList<>();
        if(teachClasses!=null) {
            teachClasses.forEach(classitem -> {
                        List<Ccs> CcsList = ccsRepository.findByClassId(classitem.getClassid());
                        if(CcsList != null) {
                            CcsList.forEach(ccsitem -> {
                                Student student = studentRepository.FindByStudentId(ccsitem.getSid());
                                List<StuCheck> stuCheckList = stuCheckRepository.FindByStudentId(student.getSid());
                                if(stuCheckList != null && student!=null){
                                    stuCheckList.forEach(stuCheckitem -> {
                                        if (stuCheckitem.getStage() == 1) {
                                            Check check = checkRepository.FindByCheckId(stuCheckitem.getCheckid());
                                            TextBook textBook = textBookRepository.FindByBookId(check.getBookid());
                                            if(check!=null && textBook !=null) {
                                                res.add(new OutExcelBook(
                                                        course.getCid(),
                                                        course.getName(),
                                                        classitem.getName(),
                                                        student.getSid(),
                                                        student.getName(),
                                                        textBook.getISBN(),
                                                        textBook.getPublisher(),
                                                        textBook.getName()
                                                ));
                                            }
                                        }
                                    });
                                }

                            });
                        }
                    }
            );
        }

        return res;
    }
}
